package com.swordliu.facebook.Utils;

import com.swordliu.facebook.pojo.entity.User;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.DigestUtils;

/**
 * secureUtils
 * Created by SwordLiu on 2017-07-05.
 */
public class SecureUtils {

    private final static Integer HASH_ITERATIONS = 2;

    private final static String HASH_ALGORITHM_NAME = "SHA-5";

    private static SecureRandomNumberGenerator random = new SecureRandomNumberGenerator();

    /**
     * 给密码加密，并生成盐
     * @param s
     * @return
     */
    public static String MD5(String s) {
        return DigestUtils.md5DigestAsHex(s.getBytes());
    }

    public static User encryptionPassword(User user) {
        //给密码加密，并且生成私盐,如果是登录用户，则会自带盐
        if (user.getSalt() == null) {
            String salt = SecureUtils.randomHex();
            user.setSalt(salt);
        }
        //默认算法是SHA-512
        DefaultHashService hashService = new DefaultHashService();
        //可通过这该方法修改默认算法
        hashService.setHashAlgorithmName(HASH_ALGORITHM_NAME);
        //设置私盐
        hashService.setPrivateSalt(new Sha512Hash(user.getSalt()));
        //是否生成公盐
        hashService.setGeneratePublicSalt(true);
        //设置公盐
        hashService.setRandomNumberGenerator(publicSalt());
        //生成hash值得迭代次数：重复加密次数
        hashService.setHashIterations(HASH_ITERATIONS);

        HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes(user.getPassword()))
                .setSalt(ByteSource.Util.bytes(user.getSalt().getBytes())).setIterations(HASH_ITERATIONS).build();

        String hex = hashService.computeHash(request).toHex();
        user.setPassword(hex);

        return user;
    }

    /**
     * 用于生成公盐，默认使用该类.设置seed
     * @return
     */
    private static SecureRandomNumberGenerator publicSalt() {
        //给随机数设置种子，让随机数不再随机，而是固定值
        random.setSeed("facebook".getBytes());
        return random;
    }

    /**
     * 获取32位的随机数，作私盐用，不能设置seed
     * @return
     */
    public static String randomHex() {
        return random.nextBytes().toHex();
    }

//    一个简单的密码加密方法
//    public class PasswordHelper {
//        private RandomNumberGenerator randomNumberGenerator =
//                new SecureRandomNumberGenerator();
//        private String algorithmName = "md5";
//        private final int hashIterations = 2;
//
//        public void encryptPassword(User user) {
//            user.setSalt(randomNumberGenerator.nextBytes().toHex());
//            String newPassword = new SimpleHash(
//                    algorithmName,
//                    user.getPassword(),
//                    ByteSource.Util.bytes(user.getCredentialsSalt()),
//                    hashIterations).toHex();
//            user.setPassword(newPassword);
////            user.getCredentialsSalt()辅助方法返回username+salt
//        }
//    }
}
